﻿using SQLite;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NewsReader.Helpers.Orm
{
    [Table("feed_items")]
    public class FeedItem
    {
        public static FeedItem FromViewModel(int feedId, ViewModels.FeedItem feedItem)
        {
            FeedItem fi = new FeedItem();
            fi.FeedId = feedId;
            fi.SyndicationId = feedItem.Id;
            fi.Uri = feedItem.Link == null ? null : feedItem.Link.ToString();
            fi.Title = feedItem.Title;
            fi.ImageUri = feedItem.ImageUriExists ? feedItem.ImageUri.ToString() : null;
            fi.Summary = feedItem.Summary;
            fi.Text = feedItem.FullText;
            fi.PublishedDate = feedItem.PublishedTime.UtcDateTime;
            
            return fi;
        }

        public FeedItem()
        {
            Created = DateTime.Now;
        }

        [PrimaryKey, AutoIncrement, Column("id")]
        public int Id { get; set; }

        [Column("synd_id")]
        public string SyndicationId { get; set; }

        [Column("feed_id")]
        public int FeedId { get; set; }

        [Column("uri")]
        public string Uri { get; set; }

        [Column("title")]
        public string Title { get; set; }

        [Column("image_uri")]
        public string ImageUri { get; set; }

        [Column("summary")]
        public string Summary { get; set; }

        [Column("text")]
        public string Text { get; set; }

        [Column("published_date")]
        public DateTime PublishedDate { get; set; }

        [Column("created_date")]
        public DateTime Created { get; set; }
    }
}
